package com.example.demos.web.controller;


import com.example.demos.dto.MarketAddDTO;
import com.example.demos.dto.MarketDTO;
import com.example.demos.entity.Supplier;
import com.example.demos.service.MarketService;
import com.example.demos.util.PageBean;
import com.example.demos.util.PageParam;
import com.example.demos.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/v2/market")
public class MarketController {

    @Autowired
    private MarketService marketService;

    @GetMapping("/listByPage")
    public Result<PageBean<List<MarketDTO>>> select(String state, PageParam pageParam) {
        PageBean<List<MarketDTO>> select = marketService.select(state, pageParam);
        return Result.ok(select);
    }

    @GetMapping("/showsupplier")
    public Result<List<Supplier>> selectSupplier(String type) {
        List<Supplier> list = marketService.selectSupplier(type);
        return Result.ok(list);
    }

    @PostMapping("/add")
    public Result<Void> insert(@RequestBody MarketAddDTO marketAddDTO, HttpSession session) {


        int insert = marketService.insert(marketAddDTO, session);
        if (insert > 0) {
            return Result.ok("订单新增成功");
        } else if (insert == -1) {
            return Result.bizErr("Bad request!");
        }
        return Result.bizErr("订单新增失败");
    }


}
